using System;
using Gtk;

namespace TracNghiem2
{
	public partial class question_maker : Gtk.Window
	{
		public question_maker () : 
				base(Gtk.WindowType.Toplevel)
		{
			this.Build ();

			questions = new DeThi ();
			question_nodes = new Gtk.NodeStore (typeof(QuestionNode));
			nv_questions.NodeStore = question_nodes;
			nv_questions.AppendColumn ("No.", new Gtk.CellRendererText (), "text", 0);
			nv_questions.AppendColumn ("Question", new Gtk.CellRendererText (), "text", 1);
			nv_questions.NodeSelection.Changed += new System.EventHandler (OnSelectionChanged);

			tv_question.WrapMode = WrapMode.Word; /* Enable wrap mode in Question text view */
		}

		public DeThi questions;
		public MainWindow main_window;
		Gtk.NodeStore question_nodes;

		protected void OnRead (object sender, EventArgs e)
		{
			FileChooserDialog open_file_dialog = new FileChooserDialog ("Open Questions Database",
			                                                           this,
			                                                           FileChooserAction.Open,
			                                                           "Cancel", ResponseType.Cancel, 
			                                                           "Open", ResponseType.Accept);
			if (open_file_dialog.Run () == (int)ResponseType.Accept) {
				if (questions.read_questions (open_file_dialog.Filename) == -1) { /* FIXME : I delete the old records for new records */
					/* FIXME : please add exceptions here */
				}
				
			}

			//question_nodes = new Gtk.NodeStore (typeof(QuestionNode));

			int i = 1;

			foreach (CauHoi question in questions.get_database()) {
				QuestionNode question_node = new QuestionNode (i.ToString (), question);
				question_nodes.AddNode (question_node);
				i++;
			}




			open_file_dialog.Destroy ();
		}

		protected void OnWrite (object sender, EventArgs e)
		{
			FileChooserDialog save_file_dialog = new FileChooserDialog ("Save Questions Database",
			                                                           this,
			                                                           FileChooserAction.Save,
			                                                           "Cancel", ResponseType.Cancel,
			                                                           "Save", ResponseType.Accept);
			if (save_file_dialog.Run () == (int)ResponseType.Accept) {
				if (questions.write_questions (save_file_dialog.Filename) == -1) {
					/* FIXME add exceptions here */
				}
			}

			save_file_dialog.Destroy ();

		}

		void OnSelectionChanged (object obj, System.EventArgs args)
		{
			NodeSelection selected_node = (Gtk.NodeSelection)obj;
			QuestionNode selected_question = (QuestionNode)selected_node.SelectedNode;

			tv_question.Buffer.Text = selected_question.question.question;
			e_a.Text = selected_question.question.answer_a;
			e_b.Text = selected_question.question.answer_b;
			e_c.Text = selected_question.question.answer_c;
			e_d.Text = selected_question.question.answer_d;

			string right_answer = selected_question.question.rigth_answer;

			if (right_answer == "a")
				rbtn_a.Active = true;
			else if (right_answer == "b")
				rbtn_b.Active = true;
			else if (right_answer == "c")
				rbtn_c.Active = true;
			else 
				rbtn_d.Active = true;
			;
		}

		protected void OnUpdate (object obj, EventArgs args)
		{
			NodeSelection selected_node = (Gtk.NodeSelection)nv_questions.NodeSelection;
			QuestionNode selected_question = (QuestionNode)selected_node.SelectedNode;


			selected_question.question.question = tv_question.Buffer.Text;
			selected_question.quest = tv_question.Buffer.Text;
			selected_question.question.answer_a = e_a.Text;
			selected_question.question.answer_b = e_b.Text;
			selected_question.question.answer_c = e_c.Text;
			selected_question.question.answer_d = e_d.Text;

			string right_answer;

			if (rbtn_a.Active == true)
				right_answer = "a";
			else if (rbtn_b.Active == true)
				right_answer = "b";
			else if (rbtn_c.Active == true)
				right_answer = "c";
			else
				right_answer = "d";

			selected_question.question.rigth_answer = right_answer;
		}

		protected void OnDelete (object sender, EventArgs e)
		{
			/* FIXME : I'm not functional
			 * If don't select any entry, it would clear, like predicted, but if otherwise... */
			tv_question.Buffer.Text = "";
			e_a.Text = "";
			e_b.Text = "";
			e_c.Text = "";
			e_d.Text = "";

			nv_questions.NodeStore.Clear ();
			//nv_questions.NodeStore.RemoveNode (nv_questions.NodeSelection.SelectedNode);
		}

		protected void OnAdd (object sender, EventArgs e)
		{
			CauHoi add_question = new CauHoi ();
			add_question.question = tv_question.Buffer.Text;
			add_question.answer_a = e_a.Text;
			add_question.answer_b = e_b.Text;
			add_question.answer_c = e_c.Text;
			add_question.answer_d = e_d.Text;
			if (rbtn_a.Active == true)
				add_question.rigth_answer = "a";
			else if (rbtn_b.Active == true)
				add_question.rigth_answer = "b";
			else if (rbtn_c.Active == true)
				add_question.rigth_answer = "c";
			else 
				add_question.rigth_answer = "d";

			questions.add_question (add_question);
			int no = questions.count ();

			QuestionNode question_node = new QuestionNode (no.ToString (), add_question);
			question_nodes.AddNode (question_node);
		}

				
	}

}

